﻿/// <reference path="jquery-1.4.2.js" />

var Eci = Eci || {}
Eci.Communication = Eci.Communication || {}

/**
 * WebSocket通信
 *
 */
Eci.Communication.Channel = function (channelName) {
  /**
   * WebSocket的IP和端口号
   */
  this.ws = null

  this.channelName = channelName

  /**
   * 连接事件
   */
  this.connected = null

  this.connection = null

  /**
   * 异常信息
   */
  this.error = null

  /**
   * 接收WebSocket信息
   */
  this.receive = null

  /**
   * WebSocket重新连接事件
   */
  this.reconnected = null

  /**
   * WebSocket关闭事件
   */
  this.disposed = null

  /**
   * WebSocket对象
   */
  this.socket = null

  /**
   * 上传文件分片id
   */
  this.actionID = 0

  /**
   * 更新上传文件进度
   */
  this.callBacks = {}

  /**
   *
   */
  this.queues = []

  /**
   *
   */
  this.transport = ['webSockets', 'longPolling']

  /**
   * 发送文件到WebSocket
   */
  this.send = function (data, callback) {
    if (this.actionID > 500) {
      this.actionID = 0
    }

    this.actionID++

    var obj = data
    obj.actionid = this.actionID

    //  更新上传文件进度
    this.callBacks[this.actionID.toString()] = null

    if (callback != null) {
      this.callBacks[this.actionID.toString()] = callback
    }

    var datastr = JSON.stringify(obj)

    try {
      //  上传文件
      this.connection[this.channelName].server.receivedMessage(datastr).fail(function (e) {
        console.error(e)
      })
    } catch (e) {
      console.error(e)
    }
  }

  /**
   * WebSocket关闭事件
   */
  this.close = function () {
    this.actionID = 0
    var that = this

    setTimeout(function () {
      that.connection.hub.stop()
      that.connection = null
    }, 500)

    this.error = null
    this.receive = null
    this.reconnected = null
    this.disposed = null
    this.connected = null
  }

  /**
   * 接收WebSocket信息事件
   */
  this.onReceive = function (data) {
    var obj = JSON.parse(data)

    if (obj.Error) {
      if (this.error) {
        this.error(obj)
      }
    } else if (obj.State === 'RESUME') {
      //续传
      if (this.receive) {
        this.receive(obj)
      }
    } else if (
      obj.State === 'UPLOAD' ||
      obj.State === 'DOWNLOAD' ||
      obj.State === 'EXTRACT' ||
      obj.State === 'EXTRACTFROMPATH' ||
      obj.State === 'EXTRACTTOGUIDFROMPATH' ||
      obj.State === 'EXTRACTTOGUID' ||
      obj.State === 'COMPLETE'
    ) {
      //更新上传进度

      var actionid = obj.actionid.toString()
      var callback = this.callBacks[actionid]

      if (callback != null) {
        callback(obj)
      }

      if (obj.State === 'EXTRACT' && this.receive) {
        //上传完成
        this.receive(obj)
      }

      if (obj.State === 'EXTRACTTOGUID' && this.receive) {
        //上传完成
        this.receive(obj)
      }

      if (obj.State === 'EXTRACTFROMPATH' && this.receive) {
        //上传完成
        this.receive(obj)
      }

      if (obj.State === 'EXTRACTTOGUIDFROMPATH' && this.receive) {
        //上传完成
        this.receive(obj)
      }

      if (obj.State === 'COMPLETE' && this.receive) {
        //上传完成
        this.receive(obj)
      }
    }
  }

  this.state = function () {
    return $.connection[this.channelName].connection.state
  }

  this.connect = function (wsUri) {
    this.ws = wsUri
    var channel = this

    this.connection = $.connection
    this.connection.hub.url = this.ws

    this.uploadHub = $.hubConnection().createHubProxy(channelName)

    try {
      this.connection[this.channelName].client.displayMessage = function (data) {
        channel.onReceive(data)
      }
    } catch (e) {
      console.error(e)

      if (channel.error != null) {
        channel.error(e)
      }
    }

    this.connection.hub
      .start({ transport: this.transport })
      .done(function (e) {
        console.log('connected from ' + channel.ws)
        if (channel.connected != null) {
          channel.connected(e)
        }
      })
      .fail(function (e) {
        console.log('connection fail')
        //if (channel.error != null) {
        //    channel.error(e);
        //}
      })

    this.connection.hub.reconnected(function (e) {
      console.log('reconnected')
      if (channel.reconnected != null) {
        channel.reconnected(e)
      }
    })

    this.connection.hub.error(function (e) {
      console.error(e)

      if (channel.error != null) {
        channel.error(e)
      }
    })

    this.connection.hub.disconnected(function (e) {
      console.log('disconnected')
      if (channel.disposed != null) {
        channel.disposed(e)
      }
    })
  }

  /**
   * WebSocket重新连接
   */
  this.reConnectServer = function () {
    //this.connection.hub.start({ transport: this.transport })
    //    .done(function () {
    //        console.log("connected");
    //        if (channel.connected != null) {
    //            channel.connected(e);
    //        }
    //    })
    //    .fail(function (e) {
    //        console.error(e);
    //    });
  }
}
